/**
 * 
 */
package instant.mail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;

/**
 * @author qiangli
 * 
 */
public final class Shortcuts {

	private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

	public static void createRecordShortcut(Context context, String shortcut, long id,
			String listname, String emails) {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setClassName(context, EtalkActivity.class.getName());
		i.addCategory(Intent.CATEGORY_BROWSABLE);
		i.addCategory(Intent.CATEGORY_DEFAULT);
		i.putExtra(EtalkIntent.EXTRA_CONTACTID, id);
		i.putExtra(EtalkIntent.EXTRA_NAME, listname);
		i.putExtra(EtalkIntent.EXTRA_EMAIL, emails);
		i.putExtra(EtalkIntent.EXTRA_SHORTCUT, true);
		//
		Intent si = createShortcutIntent(context, shortcut,
				R.drawable.shortcut_record, i);
		si.setAction(INSTALL_SHORTCUT);
		context.sendBroadcast(si);
	}

	public static void createPlaylistShortcut(Context context, String name,
			long playlist) {
		Intent i = new Intent(Intent.ACTION_EDIT);
		i.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
		i.putExtra("playlist", String.valueOf(playlist));
		//
//		Intent i = new Intent(Intent.ACTION_VIEW);
//		i.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/playlist");
//		i.putExtra("playlist", String.valueOf(playlist));
		//
		Intent si = createShortcutIntent(context, name,
				R.drawable.shortcut_playlist, i);
		si.setAction(INSTALL_SHORTCUT);
		context.sendBroadcast(si);
	}

	public static void createPlaylistsShortcut(Context context, String name,
			String listname) {
		Intent i = new Intent(context, PlaylistActivity.class);
		i.putExtra(PlaylistActivity.EXTRA_PLAYLIST, listname);
		//
		Intent si = createShortcutIntent(context, name,
				R.drawable.shortcut_playlist, i);
		si.setAction(INSTALL_SHORTCUT);
		context.sendBroadcast(si);
	}

	private static Intent createShortcutIntent(Context context, String name,
			int icon, Intent shortcutIntent) {
		Intent i = new Intent();
		i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		i.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
				context, icon);
		i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
		return i;
	}

	public static void setupShortcut(Activity context, Class<?> cls,
			String name, int icon) {
		Intent si = new Intent(context, cls);
		Intent intent = createShortcutIntent(context, name, icon,
				si);
		context.setResult(Activity.RESULT_OK, intent);
	}
}
